001    /**
002     * Java Gui Builder - A library to build GUIs using an XML file.
003     * Copyright 2002, 2003 (C) François Beausoleil
004     *
005     * This library is free software; you can redistribute it and/or
006     * modify it under the terms of the GNU Lesser General Public
007     * License as published by the Free Software Foundation; either
008     * version 2.1 of the License, or (at your option) any later version.
009     *
010     * This library is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013     * Lesser General Public License for more details.
014     *
015     * You should have received a copy of the GNU Lesser General Public
016     * License along with this library; if not, write to the Free Software
017     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018     */
019    
020    package jgb.builder;
021    
022    import org.xml.sax.EntityResolver;
023    import org.xml.sax.InputSource;
024    import org.xml.sax.SAXException;
025    
026    import java.io.IOException;
027    import java.io.InputStream;
028    import java.io.InputStreamReader;
029    
030    public class JgbEntityResolver implements EntityResolver {
031        public static final String JGB_PUBLIC_ID = "-//SOURCEFORGE/Java Gui Builder DTD version 1.0//EN";
032        public static final String JGB_SYSTEM_ID = "http://jgb.sourceforge.net/dtd/jgb.dtd";
033        private String resourceName = "/jgb/jgb.dtd";
034    
035        public JgbEntityResolver() {
036        }
037    
038        public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
039            if (null == publicId || !publicId.equals(JGB_PUBLIC_ID)) {
040                return null;
041            }
042    
043            final InputStream dtdStream = getClass().getResourceAsStream(
044                    resourceName);
045            if (null == dtdStream) {
046                return null;
047            }
048    
049            final InputSource inputSource = new InputSource();
050            inputSource.setPublicId(publicId);
051            inputSource.setByteStream(dtdStream);
052            inputSource.setCharacterStream(new InputStreamReader(dtdStream));
053            inputSource.setEncoding("iso-8859-1");
054            return inputSource;
055        }
056    
057        public void setResourceName(String resourceName) {
058            this.resourceName = resourceName;
059        }
060    }